package org.sky.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author weifx
 * @Title: DateUtils
 * @ProjectName springboot-platform
 * @Description: TODO{日期工具类}
 * @date 2019/3/13 14:30
 * @Version 1.0
 */
public class DateUtils {
    private static final SimpleDateFormat fullDef = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 日期转换字符串
     * @param date
     * @param formate
     * @return
     */
    public static String parseDate(Date date, String formate){
        if(null == formate || StringUtils.isBlank(formate)){
            return fullDef.format(date);
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formate);
            return simpleDateFormat.format(date);
        }
    }

    /**
     * 字符串转日期
     * @param date
     * @param formate
     * @return
     * @throws ParseException
     */
    public static Date parse(String date, String formate) throws ParseException {
        if(null == formate || StringUtils.isBlank(formate)){
            return fullDef.parse(date);
        }else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formate);
            return simpleDateFormat.parse(date);
        }
    }
}
